package cyou.breathe.blog.article.domain.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cyou.breathe.blog.api.tag.request.ArticleTagCreateRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagDeleteRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagQueryRequest;
import cyou.breathe.blog.api.tag.request.ArticleTagUpdateRequest;
import cyou.breathe.blog.api.tag.response.ArticleTagCreateResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagDeleteResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagQueryResponse;
import cyou.breathe.blog.api.tag.response.ArticleTagUpdateResponse;
import cyou.breathe.blog.article.domain.entity.ArticleTag;
import cyou.breathe.blog.article.infrastructure.exception.ArticleTagErrorCode;
import cyou.breathe.blog.article.infrastructure.exception.ArticleTagException;
import cyou.breathe.blog.article.infrastructure.mapper.ArticleTagMapper;
import cyou.breathe.blog.base.exception.RepoErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: breathe
 * @createTime: 2025-09-04
 */
@Service
@Slf4j
public class ArticleTagService extends ServiceImpl<ArticleTagMapper, ArticleTag> {

    @Autowired
    private ArticleTagMapper articleTagMapper;

    /**
     * 创建标签
     * @param articleTagCreateRequest
     * @return
     */
    public ArticleTagCreateResponse createTag(ArticleTagCreateRequest articleTagCreateRequest) {

        ArticleTagCreateResponse response = new ArticleTagCreateResponse();

        //幂等
        LambdaQueryWrapper<ArticleTag> queryWrapper = Wrappers.lambdaQuery(ArticleTag.class)
                .eq(ArticleTag::getTagName, articleTagCreateRequest.getTagName())
                .eq(ArticleTag::getUserId, articleTagCreateRequest.getUserId());

        ArticleTag multiTag = articleTagMapper.selectOne(queryWrapper);

        if (ObjectUtils.isNotEmpty(multiTag)) {
            response.setErrorCode(ArticleTagErrorCode.ARTICLE_TAG_IS_EXIST);
            return response;
        }
        ArticleTag articleTag = new ArticleTag();
        articleTag.createArticleTag(articleTagCreateRequest);
        if (!this.save(articleTag)) {
            response.setErrorCode(RepoErrorCode.INSERT_FAILED);
            return response;
        }
        response.setTagId(articleTag.getId());
        response.setSuccess(true);
        return response;
    }

    /**
     * 修改标签
     * @param articleTagUpdateRequest
     * @return
     */
    public ArticleTagUpdateResponse updateTag(ArticleTagUpdateRequest articleTagUpdateRequest) {
        ArticleTagUpdateResponse response = new ArticleTagUpdateResponse();

        ArticleTag articleTag = articleTagMapper.selectByUserIdAndTagId(articleTagUpdateRequest.getUserId(), articleTagUpdateRequest.getTagId());

        Assert.notNull(articleTag, () -> new ArticleTagException(ArticleTagErrorCode.ARTICLE_TAG_NOT_EXIST));
        articleTag.updateArticleTag(articleTagUpdateRequest);

        if (!this.updateById(articleTag)) {
            response.setErrorCode(RepoErrorCode.UPDATE_FAILED);
            return response;
        }

        response.setSuccess(true);
        response.setTagId(articleTag.getId());
        return response;
    }

    /**
     * 查询所有标签
     * @param articleTagQueryRequest
     * @return
     */
    public ArticleTagQueryResponse<List<ArticleTag>> queryAllTags(ArticleTagQueryRequest articleTagQueryRequest) {
        ArticleTagQueryResponse<List<ArticleTag>> response = new ArticleTagQueryResponse<>();

        LambdaQueryWrapper<ArticleTag> queryWrapper = Wrappers.lambdaQuery(ArticleTag.class)
                .eq(ArticleTag::getUserId, articleTagQueryRequest.getUserId());

        List<ArticleTag> articleTagVOList = articleTagMapper.selectList(queryWrapper);

        response.setSuccess(true);
        response.setData(articleTagVOList);
        return response;
    }

    /**
     * 删除标签
     * @param articleTagDeleteRequest
     * @return
     */
    public ArticleTagDeleteResponse deleteTag(ArticleTagDeleteRequest articleTagDeleteRequest) {
        ArticleTagDeleteResponse response = new ArticleTagDeleteResponse();

        LambdaQueryWrapper<ArticleTag> updateWrapper = Wrappers.lambdaQuery(ArticleTag.class)
                .eq(ArticleTag::getUserId, articleTagDeleteRequest.getUserId())
                .eq(ArticleTag::getId, articleTagDeleteRequest.getTagId());

        if (!this.remove(updateWrapper)) {
            response.setErrorCode(RepoErrorCode.UPDATE_FAILED);
            return response;
        }

        response.setSuccess(true);
        return response;
    }
}
